#删除原容器
echo "begin stop and delete old pg......"
docker_name=`docker ps -a | grep -E "pgsql-cqj" | awk 'NR>0 {print $2}'`
echo "old_container_name ="
echo "------->${docker_name}"
if [[ "${docker_name[@]}"  == "" ]]; then
    echo "there's no old pgsql-cqj container,continue......."
else
    #查询visitor相关的容器id 并且删除
    docker_id=`docker ps -a | grep -E "pgsql-cqj" | awk 'NR>0 {print $1}'`
    docker stop ${docker_id} 
    echo "stop pgsql-cqj end."
    docker rm ${docker_id}
    echo "rm pgsql-cqj ok."
    echo "stop and delete old pgsql-cqj container......success!"
fi
echo "====================================================="


# pull mysql images
echo "=======================start pull pgsql images=============================="
docker pull postgres
echo "=======================pull end.=============================="

# check path
pgsql_path_data="/home/docker/postgres/pgdata"
if [[ ! -d "$pgsql_path_data" ]]; then
    echo "pgsql data path not exsits,create now."
    mkdir -p ${pgsql_path_data}
else
    echo "pgsql data path exsits,do not create."
fi

#build new pgsql-cqj container
echo "=======================start build pgsql-cqj container=============================="
docker run -d \
-p 5432:5432 \
--ip 172.20.1.8 \
--network=bridge-chen \
-v /etc/localtime:/etc/localtime:ro \
-v /home/docker/postgres/pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=Chenjin1314... \
-e TZ=Asia/Shanghai \
--privileged=true \
--restart=always \
--name pgsql-cqj \
postgres:latest
echo "=======================build end.=============================="
